package chabernac.android.wordbattle.paint.boggle;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Picture;
import android.view.View;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.WordRasterContext;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.Word;
import chabernac.android.wordbattle.model.WordRaster;
import chabernac.android.wordbattle.model.iWordRasterListener;
import chabernac.android.wordbattle.paint.ColorFactory;
import chabernac.android.wordbattle.paint.iDrawable;

/**
 * http://stackoverflow.com/questions/4374141/android-drawbitmap-very-slow-when-using-argb-8888
 *
 */

public class BoggleSelectedWordsPictureDrawable extends iDrawable {
  private WordBattleView myWordBattleView;
  private final Paint myPaint = new Paint();
  private final Set<Word> myWordsAddedToBitmap = new HashSet<Word>();
  private final WordRaster myWordRaster;
  
  private Picture myPicture;

  public BoggleSelectedWordsPictureDrawable() {
    super();
    myWordRaster = WordRasterContext.getInstance().getMediator().getWordRaster();
    init();
    addListener();
  }
  
  private void init(){
    myPaint.setStrokeWidth( 18 );
    myPaint.setStyle(Style.FILL);
    myPaint.setAntiAlias(false);
    myPaint.setMaskFilter( new BlurMaskFilter( 5, BlurMaskFilter.Blur.NORMAL) );
    myPaint.setAlpha(255);
  }
  
  private void addListener(){
    myWordRaster.addListener(new WordRasterListener());
  }
  
  private void addWordToBitmap(Word aWord){
    myPicture = new Picture(myPicture);
    Canvas theCanvas = myPicture.beginRecording( myWordBattleView.getWidth(), myWordBattleView.getHeight() );
    
    myPaint.setAlpha( 150 );
    myPaint.setColor(ColorFactory.getRandomColor());
    if(!myWordsAddedToBitmap.contains(aWord)){
      Letter thePreviousLetter = null;
      float thePreviousCenterX = -1;
      float thePreviousCenterY = -1;
      for(Letter theLetter : aWord.getLetters()){
        
        float theCenterX = (float)(WordBattleView.BORDER_SIZE + (theLetter.getColumn() + 0.5) * myWordBattleView.getWidthPerColumn());
        float theCenterY = (float)(WordBattleView.BORDER_SIZE + (theLetter.getRow() + 0.5) * myWordBattleView.getHeightPerRow());
        
        if(thePreviousLetter != null){
          //draw a rectangle from the center of the previsou letter to the center of this letter
          
          theCanvas.drawLine(thePreviousCenterX, thePreviousCenterY, theCenterX, theCenterY, myPaint);
        }
        
        thePreviousLetter = theLetter;
        thePreviousCenterX = theCenterX;
        thePreviousCenterY = theCenterY;
      }
    }
  }
  
  private void createBitmap(){
    myPicture = new Picture();
    
    for(Word theWord : myWordRaster.getFoundWordsByLocalPlayer()){
      addWordToBitmap(theWord);
    }
  }

  @Override
  public void paint(Canvas aCanvas, View aView) {
    myWordBattleView = (WordBattleView)aView;
//    if(myPicture == null){
      createBitmap();
//    }
    myPaint.setAlpha(100);
    aCanvas.drawPicture( myPicture );
  }
  
  public class WordRasterListener implements iWordRasterListener {

    @Override
    public void selectionChanged(List<Letter> aSelectedLetters) {
    }

    @Override
    public void wordFound(Word aWord) {
//      addWordToBitmap(aWord);
    }
  }

}
